package common.codeGen;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
@Controller
public class GenCreateConvert {
	@Autowired
	private GenService genService;
	public String create(GenBean genBean){
		StringBuffer sb = new StringBuffer();
		try {
			List<GenColumn> tableNameList = genService.getTableColumnName(genBean.getTableName());
			if(tableNameList==null||tableNameList.size()<=0){
				throw new IllegalArgumentException();
			}
			
			String oldtableName = genBean.getTableName().substring(0,1).toLowerCase()+genBean.getTableName().substring(1);
			sb.append("package "+genBean.getPackagePath()+".service;").append("\n");
//			
//			
			sb.append("import java.util.ArrayList;").append("\n");
			sb.append("import java.util.List;").append("\n");
			
			sb.append("import "+genBean.getPackagePath()+".controller."+genBean.getTableName()+"Argument;").append("\n");
			sb.append("import "+genBean.getPackagePath()+".dto."+genBean.getTableName()+";").append("\n");
			sb.append("import java.util.ArrayList;").append("\n");

			sb.append("\n");
			sb.append("\n");
			sb.append("public class "+genBean.getTableName()+"Convert {").append("\n");
			sb.append(change(genBean.getTableName(),tableNameList));
			sb.append(convert(genBean.getTableName(),tableNameList));
			sb.append("}\n");
			sb.append("}\n");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sb.toString();
	} 
	public String convert(String tableName,List<GenColumn> columnName){
		StringBuffer sb = new StringBuffer();
		sb.append("public void objectChangeToUpdateObject("+tableName+" updateObject, "+tableName+" dataBaseObject){\n");
		for(int i=0;i<columnName.size();i++){
			GenColumn genColumn = columnName.get(i);
			String column = genColumn.getColumn_name().substring(0,1).toUpperCase()+genColumn.getColumn_name().substring(1);
			sb.append("if(updateObject.get"+column+"()!=null){\n");
			sb.append("dataBaseObject.set"+column+"(updateObject.get"+column+"());\n");
			sb.append("}\n");
		}
		return sb.toString();
	}
	
	
	public String change(String tableName,List<GenColumn> columnName){
		StringBuffer sb = new StringBuffer();
		sb.append("public Object[] changeArgumentToQuery("+tableName+"Argument argument,String sql){\n");
		sb.append("Object[] result = new Object[2];\n");
		sb.append("List<String> argList = new ArrayList<String>();\n");
		sb.append("StringBuffer sb = new StringBuffer(sql);").append("\n");
		sb.append("if(argument!=null){").append("\n");
		for(int i=0;i<columnName.size();i++){
			GenColumn genColumn = columnName.get(i);
			String column = genColumn.getColumn_name();
			String __column = column.substring(0,1).toUpperCase()+column.substring(1);
			if(column.toLowerCase().endsWith("date")){
				sb.append("if(argument.getStart"+__column+"()!=null&&!argument.getStart"+__column+"().equals(\"\")){").append("\n");
				sb.append("argList.add(argument.getStart"+__column+"());").append("\n");
				sb.append("sb.append(\" and "+column+">=?\");").append("\n");
				sb.append("}").append("\n");
				
				sb.append("if(argument.getEnd"+__column+"()!=null&&!argument.getEnd"+__column+"().equals(\"\")){").append("\n");
				sb.append("argList.add(argument.getEnd"+__column+"());").append("\n");
				sb.append("sb.append(\" and "+column+"<=?\");").append("\n");
				sb.append("}").append("\n");
				
			}else{
				sb.append("if(argument.get"+__column+"()!=null&&!argument.get"+__column+"().equals(\"\")){").append("\n");
				sb.append("argList.add(argument.get"+__column+"());").append("\n");
				sb.append("sb.append(\" and "+column+"=?\");").append("\n");
				sb.append("}").append("\n");
			}
			
		}
		sb.append("}").append("\n");
		sb.append("result[0] = argList;").append("\n");
		sb.append("result[1] = sb.toString();").append("\n");
		sb.append("return result;").append("\n");
		sb.append("}").append("\n");
		
		sb.append("\n");
		return sb.toString();
	}
}
